#! /bin/sh

#---------------------------------------------------------------------
#                                init                                 
# 
# This is called by the template init method, which is itself called
# by fw-init when a project is being initialized.  It is intended
# for creating files in the project that will remain in the project
# and are owned by the project.  Creation of (symlinks to) files
# owned by the build method should be done at bootstrap time.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

which perl >/dev/null 2>/dev/null || {
  echo "java_maven/init: fatal: can't locate perl" 1>&2
  exit 1
}

if test -z "${FW_ROOT}"
  then
    FW_ROOT=`echo "$0" | perl -pe 's%fw/template/java_maven/init$%%;'`
    export FW_ROOT
  fi

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" || {
  echo "java_maven/init: fatal: can't determine FW_ROOT" 1>&2
  echo "java_maven/init: please set the environment variable" 1>&2
  exit 1
}

. "${FW_ROOT}/share/fw/sh/fw-find.sh"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "java_maven/init" "sh/fw-exec.sh"
fw_source "java_maven/init" "sh/parse-args.sh"
fw_source "java_maven/init" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

fw_exec "build/automake/init" "$@" || fatal "java_mvn/init"

parse_args "java_maven/init" "$@"

validate_args "java_maven/init" "groupId version" ""

fw_exec "revision/$revision/ignore-files"       \
  '.classpath'                                  \
  '.package'                                    \
  '.project'                                    \
  'repository'                                  \
  '.settings'                                   \
  'target'                                      \
  '*.tar.gz'                                    \
  '*.tgz'                                       \
  'configure.ac'				\
  'Makefile.am'					\
  'pom2fw'					\
|| fatal "java_mvn/init"

fw_find "template/java_maven/Makefile_dot_am_template" makefile

cp -f "$makefile" "./Makefile.am.local" \
|| fatal "java_maven/init"

mvn archetype:generate					\
  -DarchetypeGroupId=org.apache.maven.archetypes 	\
  -DarchetypeArtifactId=maven-archetype-quickstart	\
  -DgroupId=${groupId} 					\
  -DartifactId=${name}				\
  -Dversion=${version}					\
  -DinteractiveMode=false				\
|| fatal  "java_maven/init"

(mv ./${name}/* . && rm -rf ./${name} ) \
|| fatal "java_maven/init"

